package com.notelapierdas.activities;


import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.notelapierdas.R;

public class GoogleMapsActivity extends MapActivity implements OnClickListener{

	MapView mapView;
	MapController mc;
	GeoPoint p;
	View zoomView;
	MapOverlay mapOverlay;
	List<Overlay> listOfOverlays;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_layout);

		setMapZoom();				
		displayLocation((Double)getIntent().getExtras().get("latitude"),(Double)getIntent().getExtras().get("longitude"));

	}

	public void setMapZoom() {

		mapView = (MapView) findViewById(R.id.mapView);
		LinearLayout zoomLayout = (LinearLayout) findViewById(R.id.zoom);
		zoomView = mapView.getZoomControls();
		

		zoomLayout.addView(zoomView, new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		mapView.displayZoomControls(true);
		
		zoomView.setOnClickListener((OnClickListener) this);
		
	}
	
	
	

	public void displayLocation(double latitude, double longitude) {
		mc = mapView.getController();

		p = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));

		mc.animateTo(p);
		mc.setZoom(17);
		addMarker();
		
		mapView.invalidate();
			
			
	}
	
	public void addMarker(){
		 //---Add a location marker--
		mapOverlay = new MapOverlay();
	    listOfOverlays = mapView.getOverlays();
	    listOfOverlays.clear();
	    listOfOverlays.add(mapOverlay);  
	    
	 	   
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.zoom: {
			listOfOverlays.remove(mapOverlay);
			addMarker();

			break;
		}
		
		
		// TODO Auto-generated method stub
		
	} 
	}
	
	 class MapOverlay extends com.google.android.maps.Overlay
	    {
	        @Override
	        public boolean draw(Canvas canvas, MapView mapView, 
	        boolean shadow, long when) 
	        {
	            super.draw(canvas, mapView, shadow);                   
	 
	            //---translate the GeoPoint to screen pixels---
	            Point screenPts = new Point();
	            mapView.getProjection().toPixels(p, screenPts);
	 
	            //---add the marker---
	            Bitmap bmp = BitmapFactory.decodeResource(
	                getResources(), R.drawable.pushpinresolucion);            
	            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
	            return true;
	        }
	    }


}